package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.entity.bo.HouseQueryBo;
import com.atguigu.entity.vo.HouseVo;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class FrontHouseController extends BaseController {
    @Reference
    private HouseService houseService;
    @Reference
    private CommunityService communityService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private UserFollowService userFollowService;
    @Reference
    private HouseImageService houseImageService;


    @ResponseBody
    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result findListPage(@RequestBody HouseQueryBo houseQueryBo,
                               @PathVariable("pageNum") Integer pageNum,
                               @PathVariable("pageSize") Integer pageSize){
        PageInfo<HouseVo> pageInfo = houseService.findVoListPage(pageNum, pageSize, houseQueryBo);
        return Result.ok(pageInfo);
    }

    @ResponseBody
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id, HttpSession session){
        //1. 查询房源信息
        House house = houseService.getById(id);
        //2. 查询小区信息
        Community community = communityService.getById(house.getCommunityId());
        //3. 查询经纪人列表信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(id);
        //4. 获取房产列表信息
        List<HouseImage> houseImage1List = houseImageService.findHouseImageList(id, 1);
        //isFollow : 已登录的账户对当前房源是否关注
        UserInfo userInfo = (UserInfo) session.getAttribute("existUser");
        Map<String, Object> map = new HashMap<>();
        if (null == userInfo) {
            map.put("isFollow",false);
        } else {
            boolean isFollow = userFollowService.isFollow(userInfo.getId(),id);
            map.put("isFollow",isFollow);
        }

        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List",houseImage1List);

        return Result.ok(map);
    }
}
